package com.yry.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * 2、单向加密：消息摘要算法加密————SHA，安全散列算法
 * SHA-1
 * SHA-2:SHA-224/SHA-256/SHA-384/SHA-512
 * JDK:全部
 * BC:全部
 * CC:SHA-1/SHA-256/SHA-384/SHA-512
 * @author Administrator
 *
 */
public class SHAUtils {
	/**
	 * JDK SHA1:
	 */
	public static String jdkSha1(String srcStr){
		String sha1Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA");
			md.update(srcStr.getBytes());
			
			sha1Str = Hex.encodeHexString(md.digest());//将byte数组转成16进制；
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha1Str;
		}
		return sha1Str;
	}
	/**
	 * JDK SHA2:SHA-224
	 */
	public static String jdkSha224(String srcStr){
		String sha2Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-224");
			md.update(srcStr.getBytes());
			
			sha2Str = Hex.encodeHexString(md.digest());//将byte数组转成16进制；
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	/**
	 * JDK SHA2:SHA-256
	 */
	public static String jdkSha256(String srcStr){
		String sha2Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			md.update(srcStr.getBytes());
			
			sha2Str = Hex.encodeHexString(md.digest());//将byte数组转成16进制；
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	/**
	 * JDK SHA2:SHA-384
	 */
	public static String jdkSha384(String srcStr){
		String sha2Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-384");
			md.update(srcStr.getBytes());
			
			sha2Str = Hex.encodeHexString(md.digest());//将byte数组转成16进制；
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}	
	/**
	 * JDK SHA2:SHA-512
	 */
	public static String jdkSha512(String srcStr){
		String sha2Str = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-512");
			md.update(srcStr.getBytes());
			
			sha2Str = Hex.encodeHexString(md.digest());//将byte数组转成16进制；
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}	
	/**
	 * BC SHA1:
	 */
	public static String bcSha1(String srcStr){
		Digest digest = new SHA1Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.getBytes().length);
		byte[] sha1Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(sha1Bytes, 0);
		//将byte数组转成16进制；
		String sha1Str = org.bouncycastle.util.encoders.Hex.toHexString(sha1Bytes);
		return sha1Str;
	}	
	
	/**
	 * BC SHA2:SHA-224
	 */
	public static String bcSha224(String srcStr){
		Digest digest = new SHA224Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.getBytes().length);
		byte[] sha2Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(sha2Bytes, 0);
		//将byte数组转成16进制；
		String sha2Str = org.bouncycastle.util.encoders.Hex.toHexString(sha2Bytes);
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-256
	 */
	public static String bcSha256(String srcStr){
		Digest digest = new SHA256Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.getBytes().length);
		byte[] sha2Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(sha2Bytes, 0);
		//将byte数组转成16进制；
		String sha2Str = org.bouncycastle.util.encoders.Hex.toHexString(sha2Bytes);
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-384
	 */
	public static String bcSha384(String srcStr){
		Digest digest = new SHA384Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.getBytes().length);
		byte[] sha2Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(sha2Bytes, 0);
		//将byte数组转成16进制；
		String sha2Str = org.bouncycastle.util.encoders.Hex.toHexString(sha2Bytes);
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-512
	 */
	public static String bcSha512(String srcStr){
		Digest digest = new SHA512Digest();
		digest.update(srcStr.getBytes(), 0, srcStr.getBytes().length);
		byte[] sha2Bytes = new byte[digest.getDigestSize()];
		digest.doFinal(sha2Bytes, 0);
		//将byte数组转成16进制；
		String sha2Str = org.bouncycastle.util.encoders.Hex.toHexString(sha2Bytes);
		return sha2Str;
	}	
	
	/**
	 * BC SHA1:————方法2
	 */
	public static String bcSha1S2(String srcStr){
		String sha1Str = null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(srcStr.getBytes());
			//将byte数组转成16进制；
			sha1Str = Hex.encodeHexString(md.digest());
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha1Str;
		}
		return sha1Str;
	}	
	
	/**
	 * BC SHA2:SHA-224————方法2
	 */
	public static String bcSha224S2(String srcStr){
		String sha2Str = null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA224");
			md.update(srcStr.getBytes());
			//将byte数组转成16进制；
			sha2Str = Hex.encodeHexString(md.digest());
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-256————方法2
	 */
	public static String bcSha256S2(String srcStr){
		String sha2Str = null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA256");
			md.update(srcStr.getBytes());
			//将byte数组转成16进制；
			sha2Str = Hex.encodeHexString(md.digest());
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-384————方法2
	 */
	public static String bcSha384S2(String srcStr){
		String sha2Str = null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA384");
			md.update(srcStr.getBytes());
			//将byte数组转成16进制；
			sha2Str = Hex.encodeHexString(md.digest());
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	/**
	 * BC SHA2:SHA-512————方法2
	 */
	public static String bcSha512S2(String srcStr){
		String sha2Str = null;
		try {
			Security.addProvider(new BouncyCastleProvider());
			MessageDigest md = MessageDigest.getInstance("SHA512");
			md.update(srcStr.getBytes());
			//将byte数组转成16进制；
			sha2Str = Hex.encodeHexString(md.digest());
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return sha2Str;
		}
		return sha2Str;
	}
	
	/**
	 * 最佳：简单。
	 * CC SHA1:
 	 */
	public static String ccSha1(String srcStr)  { 
		String sha1Str = DigestUtils.sha1Hex(srcStr);
		return sha1Str;	
	}
	/**
	 * 最佳：简单。
	 * CC SHA2:SHA-256
 	 */
	public static String ccSha256(String srcStr)  { 
		String sha1Str = DigestUtils.sha256Hex(srcStr);
		return sha1Str;
		
	}	
	/**
	 * 最佳：简单。
	 * CC SHA2:SHA-384
 	 */
	public static String ccSha384(String srcStr)  { 
		String sha1Str = DigestUtils.sha384Hex(srcStr);
		return sha1Str;
		
	}
	/**
	 * 最佳：简单。
	 * CC SHA2:SHA-512
 	 */
	public static String ccSha512(String srcStr)  { 
		String sha1Str = DigestUtils.sha512Hex(srcStr);
		return sha1Str;
		
	}	
}
